package models

import (
	//"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	//"github.com/astaxie/beego"
	"errors"
)

//import "github.com/astaxie/beego/orm"

//import "github.com/astaxie/beego/orm"

type Category struct {
	BaseModel
}
type CategoryNode struct {
	Id int
	Pid   int
	Title string
	Code  string
	Image string
	State int
	Lft int
	Rgt int
	Level int
	Ordering int
	Path string
}

var PqCategoryTable = "pq_test_category"
var PqPointCategoryTable = "pq_test_point_category"
func (this *Category) Insert(node interface{}) (int64, error) {
	master := GetMasterDb()
	slave  := GetSlaveDb()
	//var n interface{}

	switch node.(type) {
		default:
		case *PqTestCategory :
			n := node.(*PqTestCategory)
			//action 1
			var parent PqTestCategory
			slave.QueryTable(PqCategoryTable).Filter("id",n.Pid).One(&parent)
			var rgts []orm.Params
			slave.QueryTable(PqCategoryTable).Filter("rgt__gt",parent.Rgt).Filter("state__gt",-1).Values(&rgts)
			for _,v := range rgts{
				up := orm.Params{
					"rgt" : v["Rgt"].(int64) + 2,
				}
				if v["Level"].(int64) >= int64(parent.Level) {
					up["lft"] = v["Lft"].(int64) + 2
				}
				master.QueryTable(PqCategoryTable).Filter("id",v["Id"]).Update(up)
			}
			//action 2
			n.Rgt = parent.Rgt + 1
			n.Lft = parent.Rgt
			n.Level = parent.Level + 1
			n.State = 1
			//action 3
			parent.Rgt = parent.Rgt + 2
			master.Update(&parent)
			return master.Insert(n)
		case *PqTestPointCategory :
			n := node.(*PqTestPointCategory)
			//action 1
			var parent PqTestPointCategory
			slave.QueryTable(PqPointCategoryTable).Filter("id",n.Pid).One(&parent)
			var rgts []orm.Params
			slave.QueryTable(PqPointCategoryTable).Filter("rgt__gt",parent.Rgt).Filter("state__gt",-1).Values(&rgts)
			for _,v := range rgts{
				up := orm.Params{
					"rgt" : v["Rgt"].(int64) + 2,
				}
				if v["Level"].(int64) >= int64(parent.Level) {
					up["lft"] = v["Lft"].(int64) + 2
				}
				master.QueryTable(PqPointCategoryTable).Filter("id",v["Id"]).Update(up)
			}
			//action 2
			n.Rgt = parent.Rgt + 1
			n.Lft = parent.Rgt
			n.Level = parent.Level + 1
			n.State = 1
			//action 3
			parent.Rgt = parent.Rgt + 2
			master.Update(&parent)
			return master.Insert(n)
	}
	return int64(1),errors.New("fuck")
}

func (this *Category) Remove(id int,table string) (int64,error){
	master := GetMasterDb()
	slave  := GetSlaveDb()

	switch table {
	case "pq_test_category" :
		var node PqTestCategory
		slave.QueryTable(table).Filter("id",id).One(&node)
		//action 1,delete all child nodes
		slave.QueryTable(table).Filter("lft__gte",node.Lft).Filter("rgt__lte",node.Rgt).Update(orm.Params{
			"state" : -1,
		})
		//action 2
		var rgts []orm.Params
		slave.QueryTable(table).Filter("rgt__gt",node.Rgt).Filter("state__gt",-1).Values(&rgts)
		for _,v := range rgts{
			up := orm.Params{
				"rgt" : v["Rgt"].(int64) - 2,
			}
			if v["Level"].(int64) >= int64(node.Level) {
				up["lft"] = v["Lft"].(int64) - 2
			}
			master.QueryTable(table).Filter("id",v["Id"]).Update(up)
		}
		//action 3
		return master.QueryTable(table).Filter("id",id).Update(orm.Params{
			"state" : -1,
		})
	case "pq_test_point_category":
		var node PqTestPointCategory
		slave.QueryTable(table).Filter("id",id).One(&node)
		//action 1,delete all child nodes
		slave.QueryTable(table).Filter("lft__gte",node.Lft).Filter("rgt__lte",node.Rgt).Update(orm.Params{
			"state" : -1,
		})
		//action 2
		var rgts []orm.Params
		slave.QueryTable(table).Filter("rgt__gt",node.Rgt).Filter("state__gt",-1).Values(&rgts)
		for _,v := range rgts{
			up := orm.Params{
				"rgt" : v["Rgt"].(int64) - 2,
			}
			if v["Level"].(int64) >= int64(node.Level) {
				up["lft"] = v["Lft"].(int64) - 2
			}
			master.QueryTable(table).Filter("id",v["Id"]).Update(up)
		}
		//action 3
		return master.QueryTable(table).Filter("id",id).Update(orm.Params{
			"state" : -1,
		})
	}

	return int64(1),errors.New("fuck")
	//node.State = -1
	//return master.Update(&node)
	//master.QueryTable(PqCategoryTable).Filter("id",id).Delete()
	//master.Delete(&PqTestCategory{Id:this.Id}})
}


